home *** CD-ROM | disk | FTP | other *** search
/ Day Cry / Day Cry CD.bin / oh_towns / taropyon / splib / splib.lzh / PRG / LHX / MAIN.C < prev    next >
C/C++ Source or Header  |  1992-12-11  |  10KB  |  543 lines

  1. /***********************************************************
  2.                 mn.c -- main module of LH
  3. ***********************************************************/
  4. #include    <loader.h>
  5. #include    <fslib.h>
  6. #include    "lh386.h"
  7.  
  8. #include    <ctype.h>
  9. #include    <stdio.h>
  10. #include    <io.h>
  11. #include    <dos.h>
  12. #include    <errno.h>
  13. #include    <string.h>
  14. #include    <stdlib.h>
  15. #include    <time.h>
  16.  
  17. #include    "lh.h"
  18. #include    "errmes.h"
  19. #include    "intrface.h"
  20.  
  21. #ifdef    __HIGHC__
  22. #    pragma    On(Align_labels);
  23. #endif
  24.  
  25.  
  26. #define SECURITY 0
  27.  
  28. #define CRCPOLY  0xA001         /* CRC-16 */
  29.  
  30. char        work[4096];
  31. char        lhtmp1[] = "LHTMP$$$.)1(";
  32. char        lhtmp2[] = "LHTMP$$$.)2(";
  33. FILE       *redirect = NULL, *response = NULL;
  34. int         paramcount = -2;
  35. char        cmd;
  36. int         cmdupdate;
  37. char       *env;
  38. char       *workdir = "";
  39. char       *pager = "LESS";
  40. char       *keyword = "";
  41. char        arcname[MAX_PATH];
  42. char        filename1[MAX_PATH];
  43. char        filename2[MAX_PATH];
  44. char        filename3[MAX_PATH];
  45. FILE       *file1, *file2, *file3;
  46. int         errorlevel = 0;
  47.  
  48. #ifndef __HIGHC__
  49. static time_t arcstamp;
  50. #else
  51. static time_t arcstamp =
  52. {0};
  53. #endif
  54.  
  55. boolean     verify_mode = FALSE;
  56.  
  57. /*    :*****************************************************************
  58.     :    display usages
  59.     :*****************************************************************
  60. */
  61. void        usage(void)
  62. {
  63.     extern void dispusage(void);
  64.  
  65.     dispusage();
  66. }
  67.  
  68. void        update_arcstamp(void)
  69. {
  70.     if (arcstamp < hpb.utc)
  71.         arcstamp = hpb.utc;
  72. }
  73.  
  74. static void set_arcstamp(FILE * f)
  75. {
  76.     if (flg_t)
  77.         setfiletime(f, arcstamp);
  78. }
  79.  
  80. static char *readword(FILE * file)
  81. {
  82.     char       *p;
  83.     int         c;
  84.  
  85.     while (((c = getc(file)) & 0xFF) <= ' ');
  86.     if (c < 0)
  87.     {
  88.         FS_fclose(file);
  89.         return NULL;
  90.     }
  91.     p = work;
  92.     do
  93.     {
  94.         *p++ = c;
  95.     } while ((c = getc(file)) > ' ');
  96.     *p++ = '\0';
  97.  
  98.     return work;
  99. }
  100.  
  101. static void disparcname(char *title)
  102. {
  103.     if (flg_n == 0)
  104.     {
  105.         LHX_printf( "\n%s archive : %s\n\n", title, arcname);
  106.     }
  107. }
  108.  
  109. static void        executecmd(void)
  110. {
  111.     int         modified, err;
  112.     char       *bdir, *path;
  113.     long        size;
  114.     uchar        c;
  115.  
  116.     err = 0;
  117.     arcstamp = 0;
  118.     modified = 0;
  119.     if (cmdupdate)
  120.     {
  121.         strcpy(filename1, arcname);
  122.         strcpy(backpath(filename1), lhtmp1);
  123.         if ( FS_remove(filename1) && errno != ENOENT )
  124.             error(RDONLY, filename1);
  125.         FS_rename(arcname, filename1);
  126.     } else
  127.     {
  128.         strcpy(filename1, arcname);
  129.     }
  130.     file1 = myeopen(filename1, "rb", NULL);
  131.     if (cmdupdate || cmd == 'S')
  132.     {
  133.         if (flg_w)
  134.         {
  135.             strcpy(filename2, workdir);
  136.             convdelim(filename2, DELIM);
  137.             c = filename2[strlen(filename2) - 1];
  138.             if (c != DELIM && c != ':')
  139.                 strcat(filename2, DELIMSTR);
  140.             strcat(filename2, lhtmp2);
  141.         } else
  142.         {
  143.             strcpy(filename2, arcname);
  144.             strcpy(backpath(filename2), lhtmp2);
  145.         }
  146.         file2 = myeopen(filename2, "wb", NULL);
  147.         if (file2 == NULL)
  148.         {
  149.             file2 = myeopen(lhtmp2, "wb", NULL);
  150.             if (file2 == NULL)
  151.                 error(MKTMPERR, filename2);
  152.             strcpy(filename2, lhtmp2);
  153.         }
  154.     }
  155.     switch (cmd)
  156.     {
  157.         case 'V':
  158.             flg_x = 1;
  159.             cmd = 'L';
  160.         case 'L':
  161.             disparcname("Listing of");
  162.             initlist();
  163.             break;
  164.         case 'A':
  165.             flg_c = 1;
  166.             cmd = 'U';
  167.         case 'U':
  168.         case 'M':
  169.             if (file1)
  170.             {
  171.                 disparcname("Updating");
  172.             } else
  173.             {
  174.                 disparcname("Creating");
  175.             }
  176.             mklist();
  177.             break;
  178.         case 'F':
  179.             disparcname("Freshening");
  180.             break;
  181.         case 'D':
  182.             disparcname("Deleting from");
  183.             break;
  184.         case 'X':
  185.             cmd = 'E';
  186.         case 'E':
  187.         case 'P':
  188.             disparcname("Extracting from");
  189.             break;
  190.         case 'T':
  191.             disparcname("Testing");
  192.             verify_mode = TRUE;
  193.             break;
  194.     }
  195.  
  196.     /************/
  197.     if (file1)
  198.     {
  199.         inithdr();
  200.         while (file1 && (path = gethdr(&err)) != NULL)
  201.         {
  202.             bdir = matchpat(path);
  203.             switch (cmd)
  204.             {
  205.                 case 'L':
  206.                     if (bdir)
  207.                     {
  208.                         list();
  209.                     }
  210.                     break;
  211.                 case 'U':
  212.                 case 'M':
  213.                     modified += append();
  214.                     break;
  215.                 case 'F':
  216.                     modified += freshen(bdir);
  217.                     break;
  218.                 case 'D':
  219.                     if (bdir)
  220.                     {
  221.                         LHX_printf("Deleted %s\n", path);
  222.                         modified++;
  223.                     } else
  224.                     {
  225.                         copylzh();
  226.                     }
  227.                     break;
  228.                 case 'E':
  229.                 case 'P':
  230.                 case 'T':
  231.                     if (bdir)
  232.                     {
  233.                         extract(bdir);
  234.                     }
  235.                     break;
  236.             }
  237.             free(path);
  238.         }
  239.     } else
  240.     {
  241.         if (!cmdupdate)
  242.             error(NOARCERR, arcname);
  243.     }
  244.  
  245.     /************/
  246.  
  247.     if (!err)
  248.     {
  249.         switch (cmd)
  250.         {
  251.             case 'L':
  252.                 endlist(getfiletime(file1));
  253.                 break;
  254.             case 'U':
  255.                 modified += endappend();
  256.                 break;
  257.             case 'M':
  258.                 modified += endappend();
  259.                 deletefiles();
  260.                 break;
  261.         }
  262.     }
  263. #if SECURITY
  264.     if (cmd == 'T')
  265.     {
  266.         extern int    security(void);
  267.  
  268.         if (security())
  269.             err = 0;
  270.     }
  271. #endif
  272.     if (err)
  273.         error(BROKENARC, arcname);
  274.     if (cmdupdate)
  275.     {
  276.         if (modified == 0)
  277.         {
  278.             FS_fclose(file2);
  279.             if (file1)
  280.             {
  281.                 set_arcstamp(file1);
  282.                 FS_fclose(file1);
  283.             }
  284.             FS_remove(filename2);
  285.             FS_rename(filename1, arcname); /* restore the old archive */
  286.         } else
  287.         {
  288.             LHX_fputc('\0', file2);
  289.             size = ftell(file2);
  290.             set_arcstamp(file2);
  291.             if (file1)
  292.                 FS_fclose(file1);
  293.             FS_fclose(file2);
  294.             if ( FS_rename(filename2, arcname) )
  295.             {
  296.                 if (diskspace(filename1) < size)
  297.                     error(NOSPCERR, NULL);
  298.                 LHX_printf( "Copying TMP to ARC ... ");
  299.                 FS_remove(filename1);
  300.                 file1 = mywopen(arcname);
  301.                 file2 = myropen(filename2);
  302.                 copyfile(file2, file1, size, 0);
  303.                 set_arcstamp(file1);
  304.                 FS_fclose(file1);
  305.                 FS_fclose(file2);
  306.                 FS_remove(filename2);
  307.                 LHX_printf( "done.\n" );
  308.             } else
  309.             {
  310.                 FS_remove(filename1);
  311.             }
  312.             if (size == 1)
  313.                 FS_remove(arcname);
  314.         }
  315.     } else
  316.     {
  317.         FS_fclose(file1);
  318.     }
  319. }
  320.  
  321. static int    execute(void)
  322. {
  323.     struct find_t findbuf;
  324.     int         nofile;
  325.  
  326.     nofile = _dos_findfirst(arcname, 0x07, &findbuf);
  327.     if (nofile && strchr("AUM", cmd) == NULL)
  328.         error(NOARCERR, arcname);
  329.     if (cmdupdate)
  330.     {
  331.         if ( strpbrk(arcname, "*?") )
  332.             error(NOARCERR, arcname);
  333.         /* when updating archive, wild cards can't used */
  334.         if (!nofile && findbuf.attrib & 0x01)
  335.             error(RDONLY, arcname);
  336.         executecmd();
  337.     } else
  338.     {
  339.         do
  340.         {
  341.             backpath(arcname);
  342.             strcat(arcname, findbuf.name);
  343.             executecmd();
  344.         } while (_dos_findnext(&findbuf) == 0);
  345.     }
  346.     return errorlevel;
  347. }
  348.  
  349. #ifdef    __HIGHC__
  350. #define GETENV(env)     _hc_getenv(env)
  351. char       *_hc_getenv(const char *env)
  352. {
  353.     char       *p, *tmp;
  354.  
  355.     if ((p = getenv(env)) == NULL)
  356.         return (NULL);
  357.     if ((tmp = malloc(strlen(p) + 1)) == NULL)
  358.         return (NULL);
  359.     strcpy(tmp, p);
  360.     return (tmp);
  361. }
  362. #else
  363. #define GETENV(env)     getenv(env)
  364. #endif
  365.  
  366. jmp_buf    MnJmpBuf;
  367.  
  368. void    main( int _argc, char *_argv[] )
  369. {
  370.     uchar        c, *p, *q;
  371.     int         i, argc;
  372.     char        **argv;
  373.     static        fileinputflag;
  374.     static char *basedir;
  375.  
  376.     fileinputflag = 0;
  377.     basedir       = "";
  378.  
  379. #ifdef    _STARTUP_INIT
  380.     main_ini();
  381. #endif
  382.  
  383.     /* âVâXâeâÇÅëè·ë╗    */
  384.     if ( USYS_init( &argc, &argv, _argc, _argv ) )
  385.     {    pcl_exit(0);
  386.         exit(1);
  387.     }
  388.  
  389.     if ( (i = setjmp(MnJmpBuf)) > 0 )
  390.         goto END_EXIT;
  391.  
  392.     initbreak();
  393.     getswchar();
  394.     make_crctable(CRCPOLY);
  395.     initpat();
  396.     if ((p = (uchar *) GETENV("TMP")) != NULL
  397.         && (p = (uchar *) strtok((char *) p, " \t")) != NULL)
  398.     {
  399.         workdir = (char *) p;
  400.         flg_w = 1;
  401.     }
  402.     if ((env = GETENV("LH")) == NULL)
  403.         env = GETENV("LHARC");
  404.     argc--;
  405.     argv++;
  406.     while (1)
  407.     {
  408.         if (env)
  409.         {
  410.             while (*env && (uchar) * env <= ' ')
  411.                 env++;
  412.             if (*env == '\0')
  413.             {
  414.                 env = NULL;
  415.                 continue;
  416.             }
  417.             p = (uchar *) env;
  418.             while (*env > ' ')
  419.                 env++;
  420.             if (*env)
  421.             {
  422.                 *env++ = '\0';
  423.             }
  424.         } else if (response)
  425.         {
  426.             fileinputflag = 1;
  427.             if ((p = (uchar *) readword(response)) == NULL)
  428.             {
  429.                 response = NULL;
  430.                 continue;
  431.             }
  432.         } else if (argc)
  433.         {
  434.             argc--;
  435.             p = (uchar *) * argv++;
  436.         } else if (redirect == NULL || (p = (uchar *) readword(redirect)) == NULL)
  437.         {
  438.             break;
  439.         }
  440.         if (*p == swchar || *p == '-')
  441.         {
  442.             getopt((char *) ++p);
  443.         } else if (*p == '@')
  444.         {
  445.             p++;
  446.             if (response)
  447.                 error(NESTERR, (char *) p);
  448.             if ((response = FS_fopen( (char *) p, "rt")) == NULL)
  449.                 error(RDERR, (char *) p);
  450.             continue;
  451.         } else
  452.         {
  453.             if (paramcount == -2)
  454.             {
  455.  
  456.                 /* get comma